//
//  JPComposeButton.swift
//  JPWeibo
//
//  Created by 哇咔咔 on 2016/10/4.
//  Copyright © 2016年 jp. All rights reserved.
//

import UIKit

class JPComposeButton: UIButton {
    
    var buttonModel: JPComposeMenu? {
        didSet{
            self.setTitle(buttonModel?.title!, for: .normal)
            self.setImage(UIImage.init(named: (buttonModel?.icon)!), for: .normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    convenience init(model: JPComposeMenu, frame: CGRect) {
//        self.init(frame: frame)
//        self.setTitle(model.title, for: .normal)
//        self.setImage(UIImage.init(named: model.icon!), for: .normal)
//    }
    
    private func setupUI() {
        titleLabel?.textAlignment = .center
        
        titleLabel?.font = UIFont.systemFont(ofSize: 14)
        self.setTitleColor(UIColor.gray, for: .normal)
        
        imageView?.contentMode = .center
        
    }
    
    override var isHighlighted: Bool {
        get {
            return false
        } set {
            
        }
    }
    
    //  调整子控件布局
    override func layoutSubviews() {
        super.layoutSubviews()
        //  设置图片的大小
        imageView?.width = width
        imageView?.height = width
        imageView?.y = 0
        //  设置文字label的大小
        titleLabel?.width = width
        titleLabel?.height = height - width
        //  设置titleLabel的y为imageview的高度
        titleLabel?.y = width
        titleLabel?.x = 0
    }

    
}
